#include "ClientForm.h"
#include "ui_ClientForm.h"
#include "PluginManager.h"


using namespace std;

ClientForm::ClientForm(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::ClientForm)
{
    ui->setupUi(this);
    protocol = new ClientTCP();
    ui->buttonDisconnect->setEnabled(false);
    ui->buttonDown->setEnabled(false);
    ui->buttonLeft->setEnabled(false);
    ui->buttonRight->setEnabled(false);
    ui->buttonUp->setEnabled(false);
    PluginManager pm;
    pm.loadPlugin("/proj/virtualrobotics/svn/src/robotslib/plugin.so");
    RobotFactory* rf = pm.getRobotFactory();
    QString s(rf->getAvailableRobots());
    QStringList list = s.split(",", QString::SkipEmptyParts);
    for (int i = 0; i < list.size(); ++i)
    {
        ui->listWidget->addItem(list[i]);
    }
}

ClientForm::~ClientForm()
{
    delete ui;
}

void ClientForm::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void ClientForm::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Escape)
    {
        qApp->quit();
    }

    if (!protocol->isConnected()) return ;
    try
    {
        if (event->key() == Qt::Key_Up)
        {
            ui->buttonUp->animateClick();
            printf("UP\n");
            protocol->sendToServer(UP);
        }
        if (event->key() == Qt::Key_Down)
        {
            ui->buttonDown->animateClick();
            printf("DOWN\n");
            protocol->sendToServer(DOWN);
        }
        if (event->key() == Qt::Key_Left)
        {
            ui->buttonLeft->animateClick();
            printf("LEFT\n");
            protocol->sendToServer(LEFT);
        }
        if (event->key() == Qt::Key_Right)
        {
            ui->buttonRight->animateClick();
            printf("RIGHT\n");
            protocol->sendToServer(RIGHT);
        }
        if (event->key() == Qt::Key_0)
        {
            printf("0\n");
            protocol->sendToServer(0);
        }
        if (event->key() == Qt::Key_1)
        {
            printf("1\n");
            protocol->sendToServer(0);
        }
        if (event->key() == Qt::Key_2)
        {
            printf("2\n");
            protocol->sendToServer(0);
        }
        if (event->key() == Qt::Key_3)
        {
            printf("3\n");
            protocol->sendToServer(0);
        }
    }
    catch(NetworkException E)
    {
        QMessageBox::critical(0, "Client error", E.showReason());
        ui->buttonConnect->setEnabled(true);
        ui->buttonDisconnect->setEnabled(false);
        ui->inputIP->setEnabled(true);

        ui->buttonDown->setEnabled(false);
        ui->buttonLeft->setEnabled(false);
        ui->buttonRight->setEnabled(false);
        ui->buttonUp->setEnabled(false);
    }
}

void ClientForm::setProtocol(ClientProtocol *p)
{
    protocol = p;
}


void ClientForm::on_buttonConnect_clicked()
{
    if (ui->listWidget->currentRow() == -1)
    {
        QMessageBox::critical(0, "Client", "Choose the type");
        return ;
    }

    try
    {   
        ui->buttonConnect->setEnabled(false);
        ui->buttonDisconnect->setEnabled(true);
        ui->inputIP->setEnabled(false);


        protocol->connect(ui->inputIP->text());
        protocol->sendRobotName(ui->listWidget->currentItem()->text().toStdString().data());

        ui->buttonDown->setEnabled(true);
        ui->buttonLeft->setEnabled(true);
        ui->buttonRight->setEnabled(true);
        ui->buttonUp->setEnabled(true);

    }
    catch(NetworkException E)
    {
        QMessageBox::critical(0, "Client error", E.showReason());
        ui->buttonConnect->setEnabled(true);
        ui->buttonDisconnect->setEnabled(false);
        ui->inputIP->setEnabled(true);

        ui->buttonDown->setEnabled(false);
        ui->buttonLeft->setEnabled(false);
        ui->buttonRight->setEnabled(false);
        ui->buttonUp->setEnabled(false);
    }
}

void ClientForm::on_buttonUp_clicked()
{
    printf("UP\n");
    protocol->sendToServer(UP);
}

void ClientForm::on_buttonDown_clicked()
{
    printf("Down\n");
    protocol->sendToServer(DOWN);
}

void ClientForm::on_buttonRight_clicked()
{
    printf("Right\n");
    protocol->sendToServer(RIGHT);
}

void ClientForm::on_buttonLeft_clicked()
{
    printf("Left\n");
    protocol->sendToServer(LEFT);
}

void ClientForm::on_buttonDisconnect_clicked()
{
    protocol->close();
    ui->buttonConnect->setEnabled(true);
    ui->inputIP->setEnabled(true);

    ui->buttonDown->setEnabled(false);
    ui->buttonLeft->setEnabled(false);
    ui->buttonRight->setEnabled(false);
    ui->buttonUp->setEnabled(false);
    ui->buttonDisconnect->setEnabled(false);
}

void ClientForm::on_pushButton_clicked()
{
    QString filename = QFileDialog::getOpenFileName(this, tr("Open file"), "", tr("Files (*.*)"));
    void *handle = dlopen(filename.toAscii().data(), RTLD_LAZY);
    int (*fun)(void*) = (int (*)(void*))dlsym(handle, "function");

    (*fun)((void*)protocol);
}
